// 双击输入
function Show(element) {
  var oldhtml = element.innerHTML;
  // if (oldhtml == null || oldhtml.length == 0) {
  //   return alert("不能为空!");
  // }

  var newInput = document.createElement('input');
  newInput.type = 'text';
  newInput.value = oldhtml;
  newInput.onblur = function () {
    element.innerHTML = this.value == oldhtml ? oldhtml : this.value;
  }

  element.innerHTML = '';
  element.appendChild(newInput);
  newInput.setSelectionRange(0, oldhtml.length);
  newInput.focus();
}

// 添加表格
let add = document.querySelector('.add')
let tbody = document.querySelector('tbody')

let head = document.querySelector('.head')
add.addEventListener('click', function () {
  let tr = document.createElement('tr')
  for (let i = 0; i < head.children.length; i++) {
    let td = document.createElement('td')
    if (i == head.children.length - 2) {
      td.innerHTML = `<button class="ying">玩家赢</button>`
    }
    if (i == head.children.length - 1) {
      td.innerHTML = `<button class="shu">玩家输</button>`
    }
    tr.append(td)
  }
  tbody.append(tr)
})

// 双击修改
tbody.addEventListener('dblclick', function (event) {
  if (event.target.classList.contains('ying') || event.target.classList.contains('shu')) {
    return
  }
  Show(event.target)
})

// 单击
tbody.addEventListener('click', function (event) {
  if (event.target.classList.contains('ying')) {
    // 上局积分
    event.target.parentNode.parentNode.children[0].innerHTML = event.target.parentNode.parentNode.children[2].innerHTML

    // 当前积分控制
    event.target.parentNode.parentNode.children[2].innerHTML = Number(event.target.parentNode.parentNode.children[2].innerHTML) + (Number(event.target.parentNode.parentNode.children[4].innerHTML) + Number(event.target.parentNode.parentNode.children[5].innerHTML)) * 0.95;

    // 输赢显示
    event.target.parentNode.parentNode.children[1].innerHTML = "+" + (Number(event.target.parentNode.parentNode.children[4].innerHTML) + Number(event.target.parentNode.parentNode.children[5].innerHTML)) * 0.95;

    // 清空书铁
    event.target.parentNode.parentNode.children[4].innerHTML = "";
    event.target.parentNode.parentNode.children[5].innerHTML = "";
  }
  if (event.target.classList.contains('shu')) {
    // 上局积分
    event.target.parentNode.parentNode.children[0].innerHTML = event.target.parentNode.parentNode.children[2].innerHTML;

    // 当前积分控制
    event.target.parentNode.parentNode.children[2].innerHTML = Number(event.target.parentNode.parentNode.children[2].innerHTML) - (Number(event.target.parentNode.parentNode.children[4].innerHTML) + Number(event.target.parentNode.parentNode.children[5].innerHTML));

    // 输赢显示
    event.target.parentNode.parentNode.children[1].innerHTML = "-" + (Number(event.target.parentNode.parentNode.children[4].innerHTML) + Number(event.target.parentNode.parentNode.children[5].innerHTML));

    // 清空书铁
    event.target.parentNode.parentNode.children[4].innerHTML = "";
    event.target.parentNode.parentNode.children[5].innerHTML = "";
  }
})